#ifndef _canbus_dragonball_
#define _canbus_dragonball_
#include "igslib_iserial_manager.h"
#include "protocol_DragonBall.h"

namespace igslib
{
    class canbus_Grgn:public CCanbus
    { 
		protected:
			std::vector<char> cache;
			static int ID;
			int LocID;
			char setup_ctrl[2]={0};
        public:

		  canbus_Grgn()
		  {
		    ID++;
			LocID=ID;
			setup_ctrl[0]=1; 
		  }
          ~canbus_Grgn()
		  {
			if (ID>-1)
			ID--;
		  
		  }
	 	 
        void SetID(const int& value)
		{		
			id = value; 	
			setup_ctrl[1]=value;		
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetupCanbus(LocID,setup_ctrl);
	
		}
		
		void FlushBuffer()
		{
			
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp->FlushCanbus(LocID,target_id);			
		}

         int Send(char* a0,int a1) 
		{ 
			if (a1>8)
			 throw_igslib_exception("canbus_Grgn::Send : length of data is large ");
		    
			
			cache.clear();
            cache.assign(a0,a0+a1);
			
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			if(a1 == 8)
			{

				tmp->SetCanbus(LocID, target_id,a0);		
			}
			else
			{
				char dtmp[8]={0};
				memcpy(dtmp,a0,a1);	 
				tmp->SetCanbus(LocID, target_id,dtmp);
			}
			
			return 8;
		}
        int  ReSend() { return Send(&cache[0],cache.size()); }
		
		int Receive(char* a0,int a1) 
		{ 
			char dtmp[8];
	
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
		    int ret=tmp->GetCanbusNV(LocID,target_id, dtmp);
			
			
			if(ret == 8)
			{
				memcpy(a0,dtmp,a1);
				return a1;
			}
			return -1;
		}
	 	
    };
	int canbus_Grgn::ID = -1;
};

#endif
